﻿//----------------------------------------------------------------------
// <copyright file="ServiceBook.cs" company="O-RID">
// Copyright (c)2010 O-RID Co.,Ltd.
// </copyright>
// <author>lidawei</author>
// <summary> 
// 作成日： 2011/10/21
// </summary>
//----------------------------------------------------------------------

using System.Runtime.Serialization;
using System.ServiceModel;

namespace Orid.Management.SOHO.Book.Service
{
    using System;
    using System.Collections.Generic;

    // 注意: 如果更改此处的类名 "ServiceBook"，也必须更新 Web.config 中对 "ServiceBook" 的引用。
    /// <summary>
    /// Book 服务端
    /// </summary>
    //[ServiceBehavior(Namespace = "http://Orid.Management.SOHO.Book.Service")]
    //[DataContract(Namespace = "http://Orid.Management.SOHO.Book.Service")]
    public class ServiceBook : IServiceBook
    {
        

        /// <summary>
        /// Gets the current date time.
        /// </summary>
        /// <returns></returns>
        public DateTime GetCurrentTime()
        {
            Info("GetCurrentTime");
            return DateTime.Now;
        }

        /// <summary>
        /// Gets the book time.
        /// </summary>
        /// <returns></returns>
        public DateTime GetBookTime()
        {
            Info("GetBookTime");
            return DateTime.Now.AddHours(DataBase.GetBookConfig());
        }

        /// <summary>
        /// Adds the schedule.
        /// </summary>
        /// <param name="operatorID">The operator ID.</param>
        /// <param name="dateFrom">The date from.</param>
        /// <param name="dateTo">The date to.</param>
        /// <returns></returns>
        public bool AddSchedule(string operatorID, DateTime dateFrom, DateTime dateTo)
        {

            Info(string.Format("AddSchedule method:OPID:{0},BookFrom:{1},BookTo:{2} ", operatorID,  dateFrom.ToString("yyyy-MM-dd HH:mm:ss"), dateTo.ToString("yyyy-MM-dd HH:mm:ss")));

            bool boolResult;

            try
            {
                boolResult = DataBase.AddSchedule(operatorID, dateFrom, dateTo);
            }
            catch (Exception ex)
            {
                Error(ex.Message);
                throw;
            }

            return boolResult;

        }

        /// <summary>
        /// Removes the schedule.
        /// </summary>
        /// <param name="operatorID">The operator ID.</param>
        /// <param name="dateFrom">The date from.</param>
        /// <param name="dateTo">The date to.</param>
        /// <returns></returns>
        public bool RemoveSchedule(string operatorID, DateTime dateFrom, DateTime dateTo)
        {

            Info(string.Format("RemoveSchedule method:OPID:{0},BookFrom:{1},BookTo:{2} ", operatorID, dateFrom.ToString("yyyy-MM-dd HH:mm:ss"), dateTo.ToString("yyyy-MM-dd HH:mm:ss")));


            bool boolResult;

            try
            {
                boolResult = DataBase.RemoveSchedule(operatorID, dateFrom, dateTo);
            }
            catch (Exception ex)
            {
                 Error(ex.Message);
                throw;
            }

            return boolResult;
        }

        /// <summary>
        /// Gets the current book setting.
        /// </summary>
        /// <returns></returns>
        public  List<BookSetting>  GetCurrentBookSetting()
        {
            Info("GetCurrentBookSetting method:");

            List<BookSetting> bookSettings;

            try
            {
                bookSettings = DataBase.GetCurrentBookSetting();
            }
            catch (Exception ex)
            {
                Error(ex.Message);
                throw;
            }

            return bookSettings ;

        }


        /// <summary>
        /// Gets the book list.
        /// </summary>
        /// <param name="operatorID">The operator ID.</param>
        /// <param name="bookDate">The book data.</param>
        /// <returns></returns>
      
        public List<BookRecord> GetBookList(string operatorID,DateTime bookDate)
        {
            Info(string.Format("GetBookList method:OPID:{0},BookData:{1} ", operatorID, bookDate.ToString("yyyy-MM-dd HH:mm:ss")));

            List<BookRecord> bookList;

            try
            {
                bookList = DataBase.GetBookList(operatorID, bookDate);
            }
            catch (Exception ex)
            {
                Error(ex.Message);
                throw;
            }

            return bookList;
        }

        /// <summary>
        /// Gets the book list.
        /// </summary>
        /// <param name="operatorID">The operator ID.</param>
        /// <param name="bookDateFrom">The book date from.</param>
        /// <param name="bookDateTo">The book date to.</param>
        /// <returns></returns>
        public List<BookRecord> GetBookList(string operatorID, DateTime bookDateFrom, DateTime bookDateTo)
        {
            Info(string.Format("GetBookList method:OPID:{0},BookData:{1} ", operatorID, bookDateFrom.ToString("yyyy-MM-dd HH:mm:ss"), bookDateTo.ToString("yyyy-MM-dd HH:mm:ss")));

            List<BookRecord> bookList;

            try
            {
                bookList = DataBase.GetBookList(operatorID, bookDateFrom,bookDateTo);
            }
            catch (Exception ex)
            {
                Error(ex.Message);
                throw;
            }

            return bookList;
        }


        /// <summary>
        /// Gets the book days.
        /// </summary>
        /// <param name="operatorID">The operator ID.</param>
        /// <param name="dateFrom">The date from.</param>
        /// <param name="dateTo">The date to.</param>
        /// <returns></returns>
        public List<DateTime> GetBookDays(string operatorID, DateTime dateFrom, DateTime dateTo)
        {
            Info(string.Format("GetBookDays method:OPID:{0},DataFrom:{1},DataTo:{2} ", operatorID, dateFrom.ToString("yyyy-MM-dd HH:mm:ss"), dateTo.ToString("yyyy-MM-dd HH:mm:ss")));

            List<DateTime> bookDays;

            try
            {
                bookDays = DataBase.GetBookDays(operatorID, dateFrom, dateTo);
            }
            catch (Exception ex)
            {
                Error(ex.Message);
                throw;
            }

            return bookDays;
        }

        /// <summary>
        /// Gets the first book record.
        /// </summary>
        /// <param name="operatorID">The operator ID.</param>
        /// <param name="bookDate">The book date.</param>
        /// <returns></returns>
        public BookRecord GetNextWorkTime(string operatorID, DateTime bookDate)
        {
            Info(string.Format("GetNextWorkTime method:OPID:{0},Data:{1} ", operatorID, bookDate.ToString("yyyy-MM-dd HH:mm:ss")));

            BookRecord bookRecord = null;

            try
            {
                bookRecord = DataBase.GetNextWorkTime(operatorID, bookDate);
            }
            catch (Exception ex)
            {
                Error(ex.Message);
                throw;
            }

            return bookRecord;
        }

        /// <summary>
        /// Infoes the specified MSG.
        /// </summary>
        /// <param name="msg">The MSG.</param>
        public void Info(string msg)
        {
            new Log().Info(msg);
            
        }

        /// <summary>
        /// Errors the specified MSG.
        /// </summary>
        /// <param name="msg">The MSG.</param>
        public void Error(string msg)
        {
            new Log().Error(msg);

        }

        /// <summary>
        /// Adds the login action.
        /// </summary>
        /// <param name="operatorID">The operator ID.</param>
        /// <returns></returns>
        public bool AddLoginAction(string operatorID)
        {
            Info(string.Format("AddLoginAction method:OPID:{0} ", operatorID));


            bool boolResult;

            try
            {
                boolResult = DataBase.AddLoginAction(operatorID);
            }
            catch (Exception ex)
            {
                Error(ex.Message);
                throw;
            }

            return boolResult;
        }

        /// <summary>
        /// Adds the logout action.
        /// </summary>
        /// <param name="operatorID">The operator ID.</param>
        /// <returns></returns>
        public bool AddLogoutAction(string operatorID)
        {
            Info(string.Format("AddLogoutAction method:OPID:{0} ", operatorID));


            bool boolResult;

            try
            {
                boolResult = DataBase.AddLogoutAction(operatorID);
            }
            catch (Exception ex)
            {
                Error(ex.Message);
                throw;
            }

            return boolResult;
        }
    }
}
